﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace GeniusAdminWebApplication
{
    public partial class resourceList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<Resource> rl = null;
            string type = Request.QueryString["type"];
            if (type != null)
                type = type.Replace("#", "");
            int status = 0;
            if (type == null || type == "all")
            {
                rl = getAllResource();
                status = 0;
            }
            else if (type == "unaudited")
            {
                rl = getUnauditedResource();
                status = 1;
            }
            else if (type == "audited")
            {
                rl = getAuditedResource();
                status = 2;
            }
            else
            {
                rl = getAllResource();
                status = 0;
            }
            foreach (Resource r in rl)
            {
                Panel p_item = new Panel();
                p_item.CssClass = "resource_item";
                p_item.Controls.Add(new LiteralControl("<div class='resource_head'>"));
                p_item.Controls.Add(new LiteralControl("<span class='resource_info'>" + r.id + "</span>"));
                p_item.Controls.Add(new LiteralControl("<span class='resource_info'>" + r.resourceName + "</span>"));
                p_item.Controls.Add(new LiteralControl("<div class='resource_description'>" + r.description + "</div>"));
                p_item.Controls.Add(new LiteralControl("</div>"));
                p_item.Controls.Add(new LiteralControl("<div class='resource_action'>"));
                p_item.Controls.Add(new LiteralControl("<span class='action_item'><a href='#'>下载查看</a></span>"));
                if (status == 1)
                {
                    p_item.Controls.Add(new LiteralControl("<span class='action_item'><a href='#' onclick='auditedResource(" + r.id + ");return false;'>审核通过</a></span>"));

                }
                if (status != 0)
                {
                    p_item.Controls.Add(new LiteralControl("<span class='action_item'><a href='#' onclick='unAuditedResource(" + r.id + ");return false;'>拒绝</a></span>"));

                }
                p_item.Controls.Add(new LiteralControl("</div>"));

                ph_resource.Controls.Add(p_item);
            }
        }

        //所有资源
        private IList<Resource> getAllResource()
        {
            return new ResourceService().getAllResources();
        }

        //未审核资源
        private IList<Resource> getUnauditedResource()
        {
            return new ResourceService().getUncensoredReources();
        }

        //已审核资源
        private IList<Resource> getAuditedResource()
        {
            return new ResourceService().getCensoredReources();
        }

        //审核
        [WebMethod]
        public static string ajaxAuditedResource(int id)
        {
            ResourceService rs = new ResourceService();
            Hashtable ht = rs.censorResource(id, 1);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        //拒绝
        [WebMethod]
        public static string ajaxUnAuditedResource(int id, string reason)
        {
            ResourceService rs = new ResourceService();
            Hashtable ht = rs.censorResource(id, -1, reason);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

    }
}